﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Runtime.InteropServices;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class BillingDailyInvBLL
    {
        private static readonly IBillingCredenceDA billingCredenceDA = DataAccess.CreateBillingCredenceDA();
        private static readonly IBillingDailyInvDA billingDailyInvDA = DataAccess.CreateBillingDailyInvDA();
        private static readonly IBillingEntityDA billingEntityDA = DataAccess.CreateBillingEntityDA();
        private Dictionary<string, BillingEntityInfo> billingEntityInfoObjects;
        private static readonly ICalendarGroupDA calendarGroupDA = DataAccess.CreateCalendarGroupDA();
        private static readonly ICalendarGroupDetailDA calendarGroupDetailDA = DataAccess.CreateCalendarGroupDetailDA();
        private Dictionary<string, List<CalendarGroupDetailInfo>> calendarGroupDetailInfoObjects;
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly IContractDA contractDA = DataAccess.CreateContractDA();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IRateDA rateDA = DataAccess.CreateRateDA();
        private static readonly IRateDetailDA rateDetailDA = DataAccess.CreateRateDetailDA();
        private Dictionary<string, List<RateDetailInfo>> rateDetailObjects;
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private Dictionary<string, SkuInfo> skuInfoObjects;
        private static readonly ITariffDA tariffDA = DataAccess.CreateTariffDA();
        private static readonly ITariffDetailDA tariffDetailDA = DataAccess.CreateTariffDetailDA();
        private Dictionary<string, TariffInfo> tariffInfoObjects;
        private static readonly ITradePartnerDA tradePartnerDA = DataAccess.CreateTradePartnerDA();
        private static readonly IUserOwnerDA userOwnerDA = DataAccess.CreateUserOwnerDA();

        public BillingDailyInvBLL()
        {
            this.pubParasInfo = null;
            this.skuInfoObjects = new Dictionary<string, SkuInfo>();
            this.tariffInfoObjects = new Dictionary<string, TariffInfo>();
            this.rateDetailObjects = new Dictionary<string, List<RateDetailInfo>>();
            this.billingEntityInfoObjects = new Dictionary<string, BillingEntityInfo>();
            this.calendarGroupDetailInfoObjects = new Dictionary<string, List<CalendarGroupDetailInfo>>();
        }

        public BillingDailyInvBLL(string currentWH)
        {
            this.pubParasInfo = null;
            this.skuInfoObjects = new Dictionary<string, SkuInfo>();
            this.tariffInfoObjects = new Dictionary<string, TariffInfo>();
            this.rateDetailObjects = new Dictionary<string, List<RateDetailInfo>>();
            this.billingEntityInfoObjects = new Dictionary<string, BillingEntityInfo>();
            this.calendarGroupDetailInfoObjects = new Dictionary<string, List<CalendarGroupDetailInfo>>();
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public BillingDailyInvBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            this.skuInfoObjects = new Dictionary<string, SkuInfo>();
            this.tariffInfoObjects = new Dictionary<string, TariffInfo>();
            this.rateDetailObjects = new Dictionary<string, List<RateDetailInfo>>();
            this.billingEntityInfoObjects = new Dictionary<string, BillingEntityInfo>();
            this.calendarGroupDetailInfoObjects = new Dictionary<string, List<CalendarGroupDetailInfo>>();
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        private DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays((double) (1 - datetime.Day));
        }

        public string GenerateDailyInvBillingCredence(string ownerID, List<string> days)
        {
            string temp = string.Empty;
            int index = 0;
            string curDay = string.Empty;
            string billingCredence = string.Empty;
            for (int i = 0; i < days.Count; i++)
            {
                index = days[i].IndexOf(";");
                curDay = days[i].Substring(0, index);
                billingCredence = days[i].Substring(index + 1, 1);
                if (days[i].Substring(index + 3, 1) != "Y")
                {
                    List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
                    billingCredenceListInfo = this.GetBillingDailyInvCredenceListInfo(ownerID, curDay);
                    DataBase dataBase = new DataBase();
                    DbConnection conn = dataBase.connection;
                    conn.Open();
                    DbTransaction tran = conn.BeginTransaction();
                    try
                    {
                        bool isstatement = false;
                        bool isbillingcredence = false;
                        BillingDailyInvInfo billingDailyInvInfo = new BillingDailyInvInfo {
                            Days = curDay,
                            OwnerID = ownerID,
                            IsStatement = "N",
                            IsStorageCharges = "N",
                            CreatedBy = this.pubParasInfo.UserID,
                            UpdatedBy = this.pubParasInfo.UserID
                        };
                        if (billingDailyInvDA.GetBillingDailyInvByID(dataBase, tran, curDay, ownerID, "N", this.dataBaseID[0]) == null)
                        {
                            billingDailyInvDA.InsertBillingDailyInv(dataBase, tran, billingDailyInvInfo, this.dataBaseID);
                        }
                        else
                        {
                            isbillingcredence = true;
                            if (billingDailyInvInfo.IsStatement != "Y")
                            {
                                billingDailyInvDA.UpdateBillingDailyInv(dataBase, tran, billingDailyInvInfo, this.dataBaseID);
                            }
                            else
                            {
                                isstatement = true;
                            }
                        }
                        if (!isstatement)
                        {
                            if (isbillingcredence)
                            {
                                billingCredenceDA.DeleteBillingCredence(dataBase, tran, curDay, "InvCharges", "Inventory", "InvCharges", this.dataBaseID[0]);
                            }
                            if (((billingCredenceListInfo != null) && (billingCredenceListInfo.Count > 0)) && (billingCredenceDA.InsertBillingCredence(dataBase, tran, billingCredenceListInfo, this.dataBaseID[0]) == 0))
                            {
                                temp = temp + curDay + ",";
                            }
                        }
                        else
                        {
                            temp = temp + curDay + ",";
                        }
                        tran.Commit();
                    }
                    catch
                    {
                        tran.Rollback();
                        temp = temp + curDay + ",";
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            return temp;
        }

        public string GenerateStorageChargesBillingCredence(string days, List<string> ownerID)
        {
            int i;
            string temp = string.Empty;
            string curOwnerID = string.Empty;
            string billingCredenceCreated = string.Empty;
            string contractExpired = string.Empty;
            string expection = string.Empty;
            if ((ownerID == null) || (ownerID.Count == 0))
            {
                List<OwnerInfo> ownerListInfo = userOwnerDA.GetAuthorizedOwnerByUserID(this.pubParasInfo.UserID, this.dataBaseID[0]);
                if ((ownerListInfo != null) && (ownerListInfo.Count > 0))
                {
                    for (i = 0; i < ownerListInfo.Count; i++)
                    {
                        ownerID.Add(ownerListInfo[i].OwnerID);
                    }
                }
            }
            for (i = 0; i < ownerID.Count; i++)
            {
                curOwnerID = ownerID[i];
                string curContractExpired = string.Empty;
                string curBillingCredenceCreated = string.Empty;
                List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
                billingCredenceListInfo = this.GetBillingCredenceListInfoByContract(billingCredenceListInfo, "InvCharges", "Owner", curOwnerID, days, out curContractExpired);
                if ((billingCredenceListInfo != null) && (billingCredenceListInfo.Count > 0))
                {
                    DataBase dataBase = new DataBase();
                    DbConnection conn = dataBase.connection;
                    conn.Open();
                    DbTransaction tran = conn.BeginTransaction();
                    try
                    {
                        BillingDailyInvInfo billingDailyInvInfo = new BillingDailyInvInfo {
                            Days = days,
                            OwnerID = curOwnerID,
                            IsStatement = "N",
                            IsStorageCharges = "Y",
                            CreatedBy = this.pubParasInfo.UserID,
                            UpdatedBy = this.pubParasInfo.UserID
                        };
                        if (billingDailyInvDA.GetBillingDailyInvByID(dataBase, tran, days, curOwnerID, "Y", this.dataBaseID[0]) == null)
                        {
                            billingDailyInvDA.InsertBillingDailyInv(dataBase, tran, billingDailyInvInfo, this.dataBaseID);
                        }
                        else if (billingDailyInvInfo.IsStatement != "Y")
                        {
                            billingDailyInvDA.UpdateBillingDailyInv(dataBase, tran, billingDailyInvInfo, this.dataBaseID);
                        }
                        if ((billingCredenceListInfo != null) && (billingCredenceListInfo.Count > 0))
                        {
                            string where = " WHERE BC.DOC_TYPE = 'StorageCharges' AND BC.DOC_ID = '" + days + "' AND BC.BILLING_OBJECT_ID = '" + curOwnerID + "'";
                            List<BillingCredenceInfo> billingCredenceInfos = billingCredenceDA.GetBillingCredenceListByWhere(dataBase, tran, where, this.dataBaseID[0], this.pubParasInfo);
                            if ((billingCredenceInfos != null) && (billingCredenceInfos.Count > 0))
                            {
                                curBillingCredenceCreated = curOwnerID;
                            }
                            else
                            {
                                for (int j = 0; j < billingCredenceListInfo.Count; j++)
                                {
                                    billingCredenceListInfo[j].BillingCredenceID = commonMethod.GenNextNumStr(dataBase, tran, "BILLING_CREDENCE", this.dataBaseID[0]);
                                    billingCredenceDA.InsertBillingCredence(dataBase, tran, billingCredenceListInfo[j], this.dataBaseID);
                                }
                            }
                        }
                        if (string.IsNullOrEmpty(curBillingCredenceCreated))
                        {
                            if (!string.IsNullOrEmpty(curContractExpired))
                            {
                                contractExpired = contractExpired + curContractExpired + ",";
                            }
                        }
                        else
                        {
                            billingCredenceCreated = billingCredenceCreated + curBillingCredenceCreated + ",";
                        }
                        tran.Commit();
                    }
                    catch
                    {
                        tran.Rollback();
                        expection = expection + curOwnerID + ",";
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            if (!string.IsNullOrEmpty(expection))
            {
                temp = expection.Substring(0, expection.Length - 1) + PublicMethod.GetInstance().GetGlobalMsgResource("F9240_001", this.pubParasInfo.UserLanguage);
            }
            if (!string.IsNullOrEmpty(billingCredenceCreated))
            {
                temp = temp + billingCredenceCreated.Substring(0, billingCredenceCreated.Length - 1) + PublicMethod.GetInstance().GetGlobalMsgResource("F9240_002", this.pubParasInfo.UserLanguage);
            }
            if (!string.IsNullOrEmpty(contractExpired))
            {
                temp = temp + contractExpired.Substring(0, contractExpired.Length - 1) + PublicMethod.GetInstance().GetGlobalMsgResource("F9240_003", this.pubParasInfo.UserLanguage);
            }
            return temp;
        }

        private List<BillingCredenceInfo> GetBillingCredenceListInfo(List<BillingCredenceInfo> billingCredenceListInfo, string billingType, string billingObject, List<BillingOrderDetailInfo> billingOrderDetailListInfo)
        {
            string whLoginID = this.dataBaseID[0];
            string rateType = "Inventory";
            for (int i = 0; i < billingOrderDetailListInfo.Count; i++)
            {
                int j;
                string ownerID = billingOrderDetailListInfo[i].OwnerID;
                string skuID = billingOrderDetailListInfo[i].SkuID;
                List<RateDetailInfo> rateDetailListInfo = null;
                SkuInfo skuInfo = null;
                if (!this.skuInfoObjects.ContainsKey(ownerID + skuID))
                {
                    skuInfo = skuDA.GetSkuByID(ownerID, skuID, whLoginID);
                    if (skuInfo != null)
                    {
                        this.skuInfoObjects.Add(ownerID + skuID, skuInfo);
                    }
                }
                else
                {
                    this.skuInfoObjects.TryGetValue(ownerID + skuID, out skuInfo);
                }
                if ((skuInfo != null) && !string.IsNullOrEmpty(skuInfo.TariffID))
                {
                    if (!this.rateDetailObjects.ContainsKey(ownerID + skuID))
                    {
                        string rateID = string.Empty;
                        TariffInfo tariffInfo = tariffDA.GetTariffByID(skuInfo.TariffID, whLoginID);
                        if (tariffInfo != null)
                        {
                            if (tariffInfo.TariffType == "Fixed")
                            {
                                rateID = tariffInfo.TypeCode;
                            }
                            else if (tariffInfo.TariffType == "CalendarGroup")
                            {
                                List<CalendarGroupDetailInfo> calendarGroupDetailListInfo = null;
                                if (!this.calendarGroupDetailInfoObjects.ContainsKey(tariffInfo.TypeCode))
                                {
                                    calendarGroupDetailListInfo = calendarGroupDetailDA.GetCalendarGroupDetailByID(tariffInfo.TypeCode, whLoginID);
                                    if (calendarGroupDetailListInfo != null)
                                    {
                                        this.calendarGroupDetailInfoObjects.Add(tariffInfo.TypeCode, calendarGroupDetailListInfo);
                                    }
                                }
                                else
                                {
                                    this.calendarGroupDetailInfoObjects.TryGetValue(tariffInfo.TypeCode, out calendarGroupDetailListInfo);
                                }
                                if ((calendarGroupDetailListInfo != null) && (calendarGroupDetailListInfo.Count > 0))
                                {
                                    DateTime dt = DateTime.Now;
                                    j = 0;
                                    while (j < calendarGroupDetailListInfo.Count)
                                    {
                                        if ((dt >= calendarGroupDetailListInfo[j].StartTime) && (dt <= calendarGroupDetailListInfo[j].EndTime))
                                        {
                                            rateID = calendarGroupDetailListInfo[j].RateID;
                                            break;
                                        }
                                        j++;
                                    }
                                }
                            }
                            rateDetailListInfo = rateDetailDA.GetRateDetailByID(rateID, whLoginID);
                            if ((rateDetailListInfo != null) && (rateDetailListInfo.Count > 0))
                            {
                                this.rateDetailObjects.Add(ownerID + skuID, rateDetailListInfo);
                            }
                        }
                    }
                    else
                    {
                        this.rateDetailObjects.TryGetValue(ownerID + skuID, out rateDetailListInfo);
                    }
                }
                if ((rateDetailListInfo != null) && (rateDetailListInfo.Count > 0))
                {
                    List<RateDetailInfo> billingRateDetailListInfo = rateDetailListInfo.FindAll(rateDetailInfo => rateDetailInfo.BillingObject == billingObject).FindAll(rateDetailInfo => rateDetailInfo.RateType == rateType);
                    if ((billingRateDetailListInfo != null) && (billingRateDetailListInfo.Count > 0))
                    {
                        for (j = 0; j < billingRateDetailListInfo.Count; j++)
                        {
                            decimal total;
                            BillingCredenceInfo billingCredenceInfo;
                            decimal fixRate = 0M;
                            if (billingRateDetailListInfo[j].ChargeMode == "Fixed")
                            {
                                fixRate = billingRateDetailListInfo[j].FixedRate;
                            }
                            else
                            {
                                if (!(billingRateDetailListInfo[j].ChargeMode == "ChargeUnitLevel"))
                                {
                                    goto Label_06EB;
                                }
                                total = 0M;
                                switch (billingRateDetailListInfo[j].ChargeUnit)
                                {
                                    case "Weight":
                                        total = decimal.Parse(skuInfo.GrossWgt.ToString()) * billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070E;

                                    case "Measurement":
                                        total = decimal.Parse(skuInfo.TareWgt.ToString()) * billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070E;

                                    case "AMT":
                                        total = decimal.Parse(skuInfo.CostPrice.ToString()) * billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070E;

                                    case "Area":
                                        total = (decimal.Parse(skuInfo.Length.ToString()) * decimal.Parse(skuInfo.Width.ToString())) * billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070E;

                                    case "Quantity":
                                        total = billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070E;

                                    case "BillingTon":
                                        if (skuInfo.GrossWgt < skuInfo.Cube)
                                        {
                                            goto Label_064A;
                                        }
                                        total = decimal.Parse(skuInfo.GrossWgt.ToString()) * billingOrderDetailListInfo[i].Qty;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070E;

                                    case "Pallet":
                                    case "Location":
                                        goto Label_070E;

                                    case "FixedRate":
                                        fixRate = billingRateDetailListInfo[j].FixedRate;
                                        goto Label_070E;

                                    case "BillingPallet":
                                        total = billingOrderDetailListInfo[i].Pallet;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070E;

                                    case "BillingCase":
                                        total = billingOrderDetailListInfo[i].Case;
                                        fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                                        goto Label_070E;
                                }
                            }
                            goto Label_070E;
                        Label_064A:
                            total = decimal.Parse(skuInfo.Cube.ToString()) * billingOrderDetailListInfo[i].Qty;
                            fixRate = this.GetRateByChargeUnit(total, billingRateDetailListInfo[j]);
                            goto Label_070E;
                        Label_06EB:
                            if (billingRateDetailListInfo[j].ChargeMode == "StcTimeLevel")
                            {
                            }
                        Label_070E:
                            if ((!(fixRate != 0M) || !(billingRateDetailListInfo[j].ChargeUnit != "Pallet")) || !(billingRateDetailListInfo[j].ChargeUnit != "Location"))
                            {
                                goto Label_0E84;
                            }
                            billingCredenceInfo = new BillingCredenceInfo();
                            billingCredenceInfo.WhID = this.pubParasInfo.CurrentWH;
                            billingCredenceInfo.BillingCredenceID = commonMethod.GenNextNumStr("BILLING_CREDENCE", whLoginID);
                            billingCredenceInfo.CredenceType = billingOrderDetailListInfo[i].BillingType;
                            billingCredenceInfo.DocID = billingOrderDetailListInfo[i].DocID;
                            billingCredenceInfo.ExternalDocID = billingOrderDetailListInfo[i].ExternalDocID;
                            billingCredenceInfo.DocType = billingOrderDetailListInfo[i].BillingType;
                            billingCredenceInfo.DocLineID = 0;
                            billingCredenceInfo.OwnerID = billingOrderDetailListInfo[i].OwnerID;
                            billingCredenceInfo.SkuID = billingOrderDetailListInfo[i].SkuID;
                            billingCredenceInfo.NetWeight = decimal.Parse(skuInfo.NetWgt.ToString());
                            billingCredenceInfo.Volume = decimal.Parse(skuInfo.Cube.ToString());
                            billingCredenceInfo.GrossWeight = decimal.Parse(skuInfo.GrossWgt.ToString());
                            billingCredenceInfo.TotalNetWeight = billingCredenceInfo.NetWeight * billingOrderDetailListInfo[i].Qty;
                            billingCredenceInfo.TotalVolume = billingCredenceInfo.Volume * billingOrderDetailListInfo[i].Qty;
                            billingCredenceInfo.TotalGrossWeight = billingCredenceInfo.GrossWeight * billingOrderDetailListInfo[i].Qty;
                            if (billingObject == "BillingEntity")
                            {
                                BillingEntityInfo billingEntityInfo = new BillingEntityInfo();
                                if (!this.billingEntityInfoObjects.ContainsKey(billingRateDetailListInfo[j].BillingEntity))
                                {
                                    billingEntityInfo = billingEntityDA.GetBillingEntityByID(billingRateDetailListInfo[j].BillingEntity, whLoginID, this.pubParasInfo);
                                    if (billingEntityInfo != null)
                                    {
                                        this.billingEntityInfoObjects.Add(billingRateDetailListInfo[j].BillingEntity, billingEntityInfo);
                                    }
                                }
                                else
                                {
                                    this.billingEntityInfoObjects.TryGetValue(billingRateDetailListInfo[j].BillingEntity, out billingEntityInfo);
                                }
                                if (billingEntityInfo != null)
                                {
                                    billingCredenceInfo.BillingObjectName = billingEntityInfo.Company;
                                }
                                billingCredenceInfo.BillingObjectID = billingRateDetailListInfo[j].BillingEntity;
                            }
                            else
                            {
                                billingCredenceInfo.BillingObjectID = billingOrderDetailListInfo[i].BillingObjectID;
                                billingCredenceInfo.BillingObjectName = billingOrderDetailListInfo[i].BillingObjectName;
                            }
                            billingCredenceInfo.BillingObject = billingObject;
                            billingCredenceInfo.RateType = billingRateDetailListInfo[j].RateType;
                            billingCredenceInfo.CostType = billingRateDetailListInfo[j].Descr;
                            billingCredenceInfo.DueFromTo = billingRateDetailListInfo[j].DueFromTo;
                            billingCredenceInfo.ChargeMode = billingRateDetailListInfo[j].ChargeMode;
                            billingCredenceInfo.ChargeUnit = billingRateDetailListInfo[j].ChargeUnit;
                            billingCredenceInfo.FixedRate = billingRateDetailListInfo[j].FixedRate;
                            billingCredenceInfo.CreatedBy = this.pubParasInfo.UserID;
                            billingCredenceInfo.UpdatedBy = this.pubParasInfo.UserID;
                            billingCredenceInfo.Quantity = billingOrderDetailListInfo[i].Qty;
                            if (billingRateDetailListInfo[j].ChargePerUnit <= 0M)
                            {
                                billingRateDetailListInfo[j].ChargePerUnit = 1M;
                            }
                            switch (billingRateDetailListInfo[j].ChargeUnit)
                            {
                                case "Weight":
                                    billingCredenceInfo.BillingAmount = (billingCredenceInfo.TotalGrossWeight * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E7A;

                                case "Measurement":
                                    billingCredenceInfo.BillingAmount = (billingCredenceInfo.TotalVolume * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E7A;

                                case "AMT":
                                    billingCredenceInfo.BillingAmount = ((billingCredenceInfo.Quantity * skuInfo.CostPrice) * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E7A;

                                case "Area":
                                    billingCredenceInfo.BillingAmount = (((billingCredenceInfo.Quantity * decimal.Parse(skuInfo.Width.ToString())) * decimal.Parse(skuInfo.Length.ToString())) * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E7A;

                                case "Quantity":
                                    billingCredenceInfo.BillingAmount = (billingCredenceInfo.Quantity * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E7A;

                                case "BillingTon":
                                    if (skuInfo.GrossWgt < skuInfo.Cube)
                                    {
                                        break;
                                    }
                                    billingCredenceInfo.BillingAmount = (billingCredenceInfo.TotalGrossWeight * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E7A;

                                case "Pallet":
                                    billingCredenceInfo.BillingAmount = 0M;
                                    goto Label_0E7A;

                                case "Location":
                                    billingCredenceInfo.BillingAmount = 0M;
                                    goto Label_0E7A;

                                case "FixedRate":
                                    billingCredenceInfo.BillingAmount = billingCredenceInfo.FixedRate;
                                    goto Label_0E7A;

                                case "BillingPallet":
                                    billingCredenceInfo.Quantity = billingOrderDetailListInfo[i].Pallet;
                                    billingCredenceInfo.BillingAmount = (billingOrderDetailListInfo[i].Pallet * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E7A;

                                case "BillingCase":
                                    billingCredenceInfo.Quantity = billingOrderDetailListInfo[i].Case;
                                    billingCredenceInfo.BillingAmount = (billingOrderDetailListInfo[i].Case * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                                    goto Label_0E7A;

                                default:
                                    goto Label_0E7A;
                            }
                            billingCredenceInfo.BillingAmount = (billingCredenceInfo.TotalVolume * billingCredenceInfo.FixedRate) / billingRateDetailListInfo[j].ChargePerUnit;
                        Label_0E7A:
                            billingCredenceListInfo.Add(billingCredenceInfo);
                        Label_0E84:;
                        }
                    }
                }
            }
            return billingCredenceListInfo;
        }

        private List<BillingCredenceInfo> GetBillingCredenceListInfoByContract(List<BillingCredenceInfo> billingCredenceListInfo, string billingType, string billingObject, string billingObjectID, string days, out string curContractExpired)
        {
            string whLoginID = this.dataBaseID[0];
            string rateType = "Inventory";
            bool isCreateBillingCredence = false;
            string contractExpired = string.Empty;
            curContractExpired = string.Empty;
            DateTime dt = Convert.ToDateTime(days);
            if (dt == this.FirstDayOfMonth(dt))
            {
                string where = " WHERE  CONTRACT_TYPE = 'LongTerm' AND IS_AUDIT = 'Y' AND OWNER_ID = '" + billingObjectID + "'";
                List<ContractInfo> contractListInfo = contractDA.GetContractByWhere(where, whLoginID);
                if ((contractListInfo == null) || (contractListInfo.Count <= 0))
                {
                    return billingCredenceListInfo;
                }
                for (int i = 0; i < contractListInfo.Count; i++)
                {
                    ContractInfo contractInfo = contractListInfo[i];
                    if ((dt >= contractInfo.StartTime) && (dt <= contractInfo.EndTime))
                    {
                        int day = DateTime.DaysInMonth(dt.Year, dt.Month);
                        BillingCredenceInfo billingCredenceInfo = new BillingCredenceInfo {
                            WhID = this.pubParasInfo.CurrentWH,
                            CredenceType = "StorageCharges",
                            DocID = days,
                            OwnerID = billingObjectID,
                            DocType = "StorageCharges",
                            BillingObjectID = billingObjectID,
                            BillingObject = billingObject,
                            RateType = rateType,
                            CostType = "",
                            DueFromTo = "DueFrom",
                            ChargeMode = "",
                            ChargeUnit = "Quantity",
                            FixedRate = contractInfo.Payment,
                            CreatedBy = this.pubParasInfo.UserID,
                            UpdatedBy = this.pubParasInfo.UserID
                        };
                        if (contractInfo.SettleAccount == "ByDay")
                        {
                            billingCredenceInfo.Quantity = decimal.Parse(day.ToString());
                        }
                        else if (contractInfo.SettleAccount == "ByMonth")
                        {
                            billingCredenceInfo.Quantity = 1M;
                        }
                        billingCredenceInfo.BillingAmount = billingCredenceInfo.Quantity * billingCredenceInfo.FixedRate;
                        TradePartnerInfo tradePartnerInfo = new TradePartnerInfo();
                        tradePartnerInfo = tradePartnerDA.GetTradePartnerByID(billingObjectID, whLoginID, this.pubParasInfo);
                        if (tradePartnerInfo != null)
                        {
                            billingCredenceInfo.BillingObjectName = tradePartnerInfo.Company;
                        }
                        billingCredenceListInfo.Add(billingCredenceInfo);
                        isCreateBillingCredence = true;
                    }
                    else
                    {
                        contractExpired = billingObjectID;
                    }
                }
                if (!(isCreateBillingCredence || string.IsNullOrEmpty(contractExpired)))
                {
                    curContractExpired = contractExpired;
                }
            }
            return billingCredenceListInfo;
        }

        public DataSet GetBillingDailyInvByQueryList(BillingDailyInvQueryEntity billingDailyInvQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (billingDailyInvQueryEntity.DailyInvType != "StorageCharges")
            {
                if (!string.IsNullOrEmpty(billingDailyInvQueryEntity.BeginDate))
                {
                    sqlWhere.Add(" I.DAYS >= '" + Convert.ToDateTime(billingDailyInvQueryEntity.BeginDate).ToString("yyyy-MM-dd") + " 00:00:00'");
                }
                if (!string.IsNullOrEmpty(billingDailyInvQueryEntity.EndDate))
                {
                    sqlWhere.Add(" I.DAYS <= '" + Convert.ToDateTime(billingDailyInvQueryEntity.EndDate).ToString("yyyy-MM-dd") + " 23:59:59'");
                }
                return billingDailyInvDA.GetBillingDailyInvByQueryList(sqlWhere, billingDailyInvQueryEntity, this.dataBaseID[0], this.pubParasInfo);
            }
            if (!string.IsNullOrEmpty(billingDailyInvQueryEntity.OwnerID))
            {
                sqlWhere.Add(" O.OWNER_ID " + billingDailyInvQueryEntity.OwnerID);
            }
            return billingDailyInvDA.GetBillingDailyInvStorageChargesByQueryList(sqlWhere, billingDailyInvQueryEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        private List<BillingCredenceInfo> GetBillingDailyInvCredenceListInfo(string ownerID, string days)
        {
            string whLoginID = this.dataBaseID[0];
            List<BillingCredenceInfo> billingCredenceListInfo = new List<BillingCredenceInfo>();
            List<BillingOrderDetailInfo> billingOrderDetailListInfo = null;
            billingOrderDetailListInfo = billingDailyInvDA.GetBillingDailyInvDetailByOwnerIDAndDays(ownerID, days, this.dataBaseID[0]);
            if ((billingOrderDetailListInfo != null) && (billingOrderDetailListInfo.Count > 0))
            {
                billingCredenceListInfo = this.GetBillingCredenceListInfo(billingCredenceListInfo, "InvCharges", "Owner", billingOrderDetailListInfo);
            }
            return billingCredenceListInfo;
        }

        private decimal GetRateByChargeUnit(decimal total, RateDetailInfo rateDetailInfo)
        {
            if ((total >= rateDetailInfo.CucFrom01) && (total <= rateDetailInfo.CucTo01))
            {
                return rateDetailInfo.CucRate01;
            }
            if ((total >= rateDetailInfo.CucFrom02) && (total <= rateDetailInfo.CucTo02))
            {
                return rateDetailInfo.CucRate02;
            }
            if ((total >= rateDetailInfo.CucFrom03) && (total <= rateDetailInfo.CucTo03))
            {
                return rateDetailInfo.CucRate03;
            }
            if ((total >= rateDetailInfo.CucFrom04) && (total <= rateDetailInfo.CucTo04))
            {
                return rateDetailInfo.CucRate04;
            }
            if ((total >= rateDetailInfo.CucFrom05) && (total <= rateDetailInfo.CucTo05))
            {
                return rateDetailInfo.CucRate05;
            }
            if ((total >= rateDetailInfo.CucFrom06) && (total <= rateDetailInfo.CucTo06))
            {
                return rateDetailInfo.CucRate06;
            }
            if ((total >= rateDetailInfo.CucFrom07) && (total <= rateDetailInfo.CucTo07))
            {
                return rateDetailInfo.CucRate07;
            }
            if ((total >= rateDetailInfo.CucFrom08) && (total <= rateDetailInfo.CucTo08))
            {
                return rateDetailInfo.CucRate08;
            }
            if ((total >= rateDetailInfo.CucFrom09) && (total <= rateDetailInfo.CucTo09))
            {
                return rateDetailInfo.CucRate09;
            }
            if ((total >= rateDetailInfo.CucFrom10) && (total <= rateDetailInfo.CucTo10))
            {
                return rateDetailInfo.CucRate10;
            }
            return 0M;
        }

        private DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays((double) (1 - datetime.Day)).AddMonths(1).AddDays(-1.0);
        }
    }
}

